package com.qianbao.controller.v1.system;


import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.svresut.Result;
import com.qianbao.components.svversion.ApiVersion;
import com.qianbao.domain.system.AppConfigEntity;
import com.qianbao.domain.system.AppUpdateEntity;
import com.qianbao.service.system.SVSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.HashMap;

@Controller
@ApiVersion(1)
@RequestMapping("/systemcenter/{version}/")
public class SystemController {


    @Autowired
    private SVSystemService service;


    /**
     * 获取系统时间
     * @return
     */
    @ResponseBody
    @RequestMapping("/system-time")
    public Result attachSystemTime(){

        HashMap map = new HashMap();
        map.put("time",service.findSystemTime());

        return Result.success(map);
    }


    @ResponseBody
    @RequestMapping("/system-app-config")
    public Result attachAppConfig(String version){
        try {
            AppConfigEntity appConfig = service.findSystemAppConfig(version);

            if (appConfig == null){
                return Result.success(Collections.emptyMap());
            }

            return Result.success(appConfig);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }


    @ResponseBody
    @RequestMapping("/system-update")
    public Result attachNeedUpdate(String version){

        try {
            AppUpdateEntity updateEntity = service.findSystemUpdate(version);

            if (updateEntity == null){
                return Result.success(Collections.emptyMap());
            }

            return Result.success(updateEntity);

        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }








}
